//
//  RWNewsChannelView.m
//  Chat
//
//  Created by 任伟 on 16/1/4.
//  Copyright © 2016年 任伟. All rights reserved.
//

#import "RWNewsChannelView.h"
#import "RWNewsChannelCell.h"
#import "RWChannelModel.h"

#define kChannelCellID @"ChannelCellID"

@interface RWNewsChannelView() <UICollectionViewDataSource>

@property (nonatomic, strong) NSMutableArray *channelModels;

@end

@implementation RWNewsChannelView

// 初始化 collectionView 的方法
- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout {
    
    self = [super initWithFrame:frame collectionViewLayout:layout];
    self.backgroundColor = [UIColor greenColor];
    
    self.dataSource = self;
    
    // 注册 cell
    [self registerClass:[RWNewsChannelCell class] forCellWithReuseIdentifier:kChannelCellID];
    
    return self;
}
//-(instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
//{
//    self = [super initWithFrame:frame collectionViewLayout:layout];
//    self.backgroundColor = [UIColor greenColor];
//    
//    // 取消滚动条
//    self.showsHorizontalScrollIndicator = NO;
//    
//    // 取消弹簧效果
//    self.bounces = NO;
//    
//    
//    // 设置数据源
//    self.dataSource = self;
//    
//    // 获得数据()
//    [self getServerData];
//    
//    // collectionView 添加 cell 需要注册一下 cell
//    [self registerClass:[RWNewsChannelCell class] forCellWithReuseIdentifier:kChannelCellID];
//    
//    return self;
//}

/// 从服务器频道获取新闻频道数据
- (void)getServerData {

    // 创建网络请求!
    NSURL *url = [NSURL URLWithString:@"http://localhost/topic_news.json"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        // 在这里 data 就是新闻频道的数据!
        if (data) {
            
            NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
            
            //            NSLog(@"%@",dict[@"tList"]);
            
            NSArray *channelArray = dataDict[@"tList"];
            
            [channelArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                // obj 就是新闻频道的字典数据
                NSDictionary *dict = obj;
                
                NSLog(@"%@",dict);
                
                // 字典转模型
                RWChannelModel *channelModel = [RWChannelModel channelWithDict:dict];
                
                // 将数据模型添加到数据源中
                [self.channelModels addObject:channelModel];
            }];
            
            // 刷新数据
            [self reloadData];
            
        }
    }];
    
    // 发送网络请求!
    
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 30;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    RWNewsChannelCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kChannelCellID forIndexPath:indexPath ];
    
    return cell;
}

//- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
//    
//    return 30;
//}
//
//- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
//    
//    RWNewsChannelCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kChannelCellID forIndexPath:indexPath ];
//    
//    return cell;
//}

#pragma mark - 懒加载数据源
- (NSMutableArray *)channelModels {

    if (_channelModels == nil) {
        _channelModels = [NSMutableArray array];
    }
    return _channelModels;
}

@end
